import { AdManager } from "../ads/AdManager";
import { EVENT_NAME_ENUM } from "../base/EnumIndex";
import { AudioManager } from "../manager/AudioManager";
import { EventManager } from "../manager/EventManager";
import { LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LockLvUI extends cc.Component {
    @property({ type: [cc.SpriteFrame], displayName: "解锁次数图片" })
    lockSprArr: Array<cc.SpriteFrame> = [];
    @property({ type: cc.Node, displayName: "背景" })
    bgNode: cc.Node = null;
    @property({ type: cc.Node, displayName: "viewNode节点" })
    viewNode: cc.Node = null;
    @property({ type: cc.Node, displayName: "解锁次数图片" })
    lockNumSpr: cc.Node = null;

    @property({ type: cc.Node, displayName: "提前体验按钮" })
    lockBtn: cc.Node = null;
    @property({ type: cc.Node, displayName: "解锁全部按钮" })
    allLockBtn: cc.Node = null;

    @property({ type: cc.Node, displayName: "背景关闭" })
    closeBtn: cc.Node = null;
    @property({ type: cc.Node, displayName: "关闭按钮" })
    noBtn: cc.Node = null;

    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
        this.updateVideoNum();
        AdManager.naoLiReportVideo(1, "解锁关卡");
    }
    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
    }
    openUI(fromUnlockAll: boolean) {
        if (!fromUnlockAll) return;
        this.lockBtn.active = false;
        this.allLockBtn.x = 0;
    }
    clickBtnClose() {
        AudioManager.playClick();
        UIManager.ins.close();
    }
    clickBtnUnlock() {
        AudioManager.playClick();
        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
        AdManager.reportVideo(3, idx, LevelManager.ins.curLvInfo.name);
        AdManager.naoLiReportVideo(2, "解锁关卡");
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
            AdManager.naoLiReportVideo(3, "解锁关卡");
            AdManager.naoLiReportVideo(4, "解锁关卡");

            LevelManager.ins.unlockCurrentLevelTemp();
            EventManager.ins.emit(EVENT_NAME_ENUM.UNLOCK_LEVEL);
            UIManager.ins.close();
        }, () => {
            UIManager.ins.enabledClick(true);
        }, null, 1);
    }
    clickBtnUnlockAll() {
        AudioManager.playClick();
        AdManager.reportVideo(3, 0, "外部关卡");
        AdManager.naoLiReportVideo(2, "解锁关卡");
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(0, "外部关卡");
            this.unlockAll();
            AdManager.naoLiReportVideo(3, "解锁关卡");
            AdManager.naoLiReportVideo(4, "解锁关卡");
        }, () => {
            UIManager.ins.enabledClick(true);
        }, 3 - LevelManager.ins.unlockAllVideoNum, 1);
    }

    updateVideoNum() {
        this.lockNumSpr.getComponent(cc.Sprite).spriteFrame = this.lockSprArr[LevelManager.ins.unlockAllVideoNum];
        cc.log("观看广告次数： " + LevelManager.ins.unlockAllVideoNum);
    }
    /** 解锁所有关卡广告 */
    unlockAll() {
        LevelManager.ins.watchAllVideo();
        this.updateVideoNum();
        if (LevelManager.ins.isUnlockAll()) {
            EventManager.ins.emit(EVENT_NAME_ENUM.UNLOCK_ALL_LEVEL);
            UIManager.ins.close();
        }
    };
}